﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Publish_Product : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(AppDomain.CurrentDomain.BaseDirectory + @"App_Data\productcategory.xml");
            DataTable dt = ds.Tables[0];
            ddlProduct.Items.Clear();
            ddlProduct.Items.Add(new ListItem("-- tất cả --", ""));
            foreach (DataRow dr in dt.Rows)
                ddlProduct.Items.Add(new ListItem(dr["Title"].ToString(), dr["IDCategory"].ToString()));
            dt.Dispose();
            ds.Dispose();
            string id = "";
            if (Request.Params["id"] != null)
                id = Request.Params["id"].ToString();
            int i = 0;
            for (i = 0; i < ddlProduct.Items.Count; i++)
                if (ddlProduct.Items[i].Value == id)
                {
                    ddlProduct.SelectedIndex = i;
                    break;
                }
            lstProduct.DataSource = GetListOfProducts(id);
            lstProduct.DataBind();
        }
    }

    protected string BindProduct(string id, string title, string image, string shortText, object price, object realPrice, object productType)
    {
        string rs = "";
        double p = Convert.ToDouble(price), r = Convert.ToDouble(realPrice);
        int type = Convert.ToInt32(productType);
        // type=1: sales off; type=2: new arrival; type=3 hot product
        if (image != "")
        {
            rs += "<table align='center'><tr><td style='padding: 4px;'><a title='" + shortText + "' href='ProductDetail.aspx?id=" + id + "'>";
            rs += "<img alt='" + shortText + "' src='ProductThumbnail.aspx?id=" + id + "&width=110' border='0' align='left' onerror=\"this.src='../images/noimage90.gif'\" />";
            rs += "</a></td></tr></table>";
        }
        rs += "<p class='title'><a title='" + shortText + "' href='ProductDetail.aspx?id=" + id + "'>" + title + "</a>";
        if (type == 1)
            rs += " <img alt='' border='0' class='icon' src='../images/sale.gif' /></p>";
        else if (type == 2)
            rs += " <img alt='' border='0' class='icon' src='../images/new.gif' /></p>";
        else if (type == 3)
            rs += " <img alt='' border='0' class='icon' src='../images/hot.gif' /></p>";
        else
            rs += "</p>";
        if (p != 0)
        {
            if (r != 0 && r < p)
            {
                rs += "<p class='price'><s>" + p.ToString("##,###") + " VNĐ</s><br><span>" + r.ToString("##,###") + " VNĐ</span></p>";
            }
            else
                rs += "<p class='price'><span>" + p.ToString("##,###") + " VNĐ</span></p>";
        }
        else if (p == 0)
            rs += "<p class='price'>Giá liên hệ</p>";
        return rs;
    }

    protected void btnView_ServerClick(object sender, EventArgs e)
    {
        string id = "";
        id = ddlProduct.SelectedValue;
        if (id == "")
        {
            Response.Redirect(@"Product.aspx", false);
        }
        else
        {
            Response.Redirect(@"Product.aspx?id=" + id, false);
        }
    }

    private DataTable GetListOfProducts(string category)
    {
        DataTable dt = null;
        try
        {
            GssBL.BLProduct BL = new GssBL.BLProduct();
            dt = BL.getProductByCategory(category, 0, "");
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            dt = null;
        }
        return dt;
    }
}
